<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="患者姓名" prop="hzName">
        <el-input
          v-model="queryParams.hzName"
          placeholder="请输入患者姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="诊断号" prop="hzNum">
        <el-input
          v-model="queryParams.hzNum"
          placeholder="请输入诊断号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['medical:Medical:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['medical:Medical:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['medical:Medical:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['medical:Medical:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="MedicalList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50" label="序号" align="center" />
      <el-table-column
        label="患者姓名"
        width="180"
        align="center"
        prop="hzName"
      />
      <el-table-column label="病历号" align="center" prop="blNum" />
      <el-table-column label="主诉" align="center" prop="blChief" />
      <el-table-column label="既病史" align="center" prop="blMedical" />
      <el-table-column label="过敏史" align="center" prop="blAllergies" />
      <el-table-column label="疾病名" align="center" prop="blIcd0" />
      <el-table-column
        label="发病时间"
        align="center"
        prop="blOnsettime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.blOnsettime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['medical:Medical:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['medical:Medical:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改病历表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="患者姓名" prop="hzName">
          <el-input v-model="form.hzName" placeholder="请输入患者姓名" />
        </el-form-item>
        <el-form-item label="主诉" prop="blChief">
          <el-input v-model="form.blChief" placeholder="请输入主诉" />
        </el-form-item>
        <el-form-item label="现病史" prop="blIllness">
          <el-input v-model="form.blIllness" placeholder="请输入现病史" />
        </el-form-item>
        <el-form-item label="现病诊疗情况" prop="blTreatment">
          <el-input
            v-model="form.blTreatment"
            placeholder="请输入现病诊疗情况"
          />
        </el-form-item>
        <el-form-item label="既病史" prop="blMedical">
          <el-input v-model="form.blMedical" placeholder="请输入既病史" />
        </el-form-item>
        <el-form-item label="过敏史" prop="blAllergies">
          <el-input v-model="form.blAllergies" placeholder="请输入过敏史" />
        </el-form-item>
        <el-form-item label="体格检查" prop="blPhysical">
          <el-input v-model="form.blPhysical" placeholder="请输入体格检查" />
        </el-form-item>
        <el-form-item label="发病时间" prop="blOnsettime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.blOnsettime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择发病时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="病历号" prop="blNum">
          <el-input v-model="form.blNum" placeholder="请输入病历号" />
        </el-form-item>
        <el-form-item label="疾病名0" prop="blIcd0">
          <el-input v-model="form.blIcd0" placeholder="请输入疾病名0" />
        </el-form-item>
        <el-form-item label="疾病名1" prop="blIcd1">
          <el-input v-model="form.blIcd1" placeholder="请输入疾病名1" />
        </el-form-item>
        <el-form-item label="疾病名2" prop="blIcd2">
          <el-input v-model="form.blIcd2" placeholder="请输入疾病名2" />
        </el-form-item>
        <el-form-item label="疾病名3" prop="blIcd3">
          <el-input v-model="form.blIcd3" placeholder="请输入疾病名3" />
        </el-form-item>
        <el-form-item label="疾病名4" prop="blIcd4">
          <el-input v-model="form.blIcd4" placeholder="请输入疾病名4" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMedical,
  getMedical,
  delMedical,
  addMedical,
  updateMedical,
} from "@/api/medical/Medical";

export default {
  name: "Medical",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 病历表表格数据
      MedicalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hzName: null,
        hzNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询病历表列表 */
    getList() {
      this.loading = true;
      listMedical(this.queryParams).then((response) => {
        this.MedicalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        hzName: null,
        blChief: null,
        blIllness: null,
        blTreatment: null,
        blMedical: null,
        blAllergies: null,
        blPhysical: null,
        blOnsettime: null,
        blNum: null,
        blIcd0: null,
        blIcd1: null,
        blIcd2: null,
        blIcd3: null,
        blIcd4: null,
        hzNum: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加病历表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getMedical(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改病历表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateMedical(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMedical(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除病历表编号为"' + ids + '"的数据项？')
        .then(function () {
          return delMedical(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "medical/Medical/export",
        {
          ...this.queryParams,
        },
        `Medical_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep {
  .pagination-container .el-pagination {
    width: 100%;
    text-align: center;
  }
}
</style>
